<template>
  <scroll
    class="listview"
    ref="listview"
    :data="data"
    :probe-type="probeType"
    @scroll="scroll"
    :listen-scroll="listenScroll"
  >
    <ul>
      <li
        class="list-group"
        ref="listGroup"
        v-for="(group, index) in data"
        :key="index"
      >
        <h2 class="list-group-title">{{ group.title }}</h2>
        <ul>
          <li
            @click="selectItem(item)"
            class="list-group-item"
            v-for="(item, index) in group.items"
            :key="index"
          >
            <img class="avatar" v-lazy="item.avatar" />
            <span class="name">{{ item.name }}</span>
          </li>
        </ul>
      </li>
    </ul>

    <div
      class="list-shortcut"
      @touchstart.stop.prevent="onShortcutTouchStart"
      @touchmove.stop.prevent="onShortcutTouchMove"
      @touchend.stop
      v-if="shortcutList.length"
    >
      <ul>
        <li
          class="item"
          v-for="(item, index) in shortcutList"
          :key="index"
          :data-index="index"
          ref="anchor"
          :class="{ current: currentIndex === index }"
        >
          {{ item }}
        </li>
      </ul>
    </div>

    <div class="list-fixed" ref="fixed" v-show="fixedTitle">
      <div class="fixed-title">{{ fixedTitle }}</div>
    </div>

    <div class="loading-wrap" v-if="!data.length">
      <loading></loading>
    </div>
  </scroll>
</template>
<script>
import Scroll from 'common/scroll/scroll'
import Loading from 'common/loading/loading'
import { getData } from 'assets/js/dom'

const TITLE_HEIGHT = 30
const ANCHOR_HEIGHT = 18

export default {
  name: 'listview',
  props: {
    data: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      scrollY: 0,
      currentIndex: 0,
      diff: 0
    }
  },
  created() {
    this.probeType = 3
    this.listenScroll = true
    this.listHeight = []
  },
  computed: {
    shortcutList() {
      return this.data.map(group => group.title.substr(0, 1))
    },
    fixedTitle() {
      if (this.scrollY > 0) {
        return ''
      } else {
        return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
      }
    },

  },
  methods: {

    //求元素高度数组
    _calculateHeight() {
      const list = this.$refs.listGroup
      let height = 0
      this.listHeight = []
      this.listHeight.push(height)
      list.forEach(item => {
        height += item.clientHeight
        this.listHeight.push(height)
      })
    },

    //监听滚动事件
    scroll(pos) {
      this.scrollY = pos.y
    },

    onShortcutTouchStart(e) {
      let anchorIndex = getData(e.target, 'index')
      this._scrollTo(anchorIndex)
    },
    onShortcutTouchMove(e) {
      let movePos = e.touches[0].pageY || e.touches[0].clientY

      let anchorIndex = parseInt((movePos - this.firstAnchorPos) / ANCHOR_HEIGHT)

      if (this.currentIndex === anchorIndex) {
        return
      }
      this._scrollTo(anchorIndex)
    },

    _scrollTo(anchorIndex) {
      if (anchorIndex < 0 || anchorIndex > this.$refs.anchor.length - 1 || anchorIndex == undefined) {
        return
      }

      let nextElement = this.$refs.listGroup[anchorIndex]
      this.$refs.listview.scrollToElement(nextElement, 0)
      this.scrollY = this.$refs.listview.scroll.y
    },

    selectItem(item) {
      this.$emit('select', item)
    },

    refresh() {
      this.$refs.listview.refresh()
    }
  },
  watch: {
    data() {
      setTimeout(() => {
        console.log(this.data);
        this._calculateHeight()

        // 第一个锚点的位置相对于视口的位置
        this.firstAnchorPos = this.$refs.anchor[0].getBoundingClientRect().y
      }, 20)
    },

    // 计算 currentIndex 值
    scrollY(newY) {
      const listHeight = this.listHeight

      if (newY > 0) {
        newY = 0
      }

      for (let i = 0; i < listHeight.length - 1; i++) {
        let height1 = listHeight[i]
        let height2 = listHeight[i + 1]
        if (-newY >= height1 && -newY < height2) {
          if (this.currindex !== i) {
            this.currentIndex = i
          }
          if (this.diff !== height2 + newY) {
            this.diff = height2 + newY
          }
          return
        }
      }

      this.currentIndex = listHeight.length - 2
    },

    diff(newVal) {
      const fixedTop = (newVal >= 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0

      if (this.fixedTop === fixedTop) {
        return
      }

      this.fixedTop = fixedTop

      this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`

    }
  },
  components: {
    Scroll,
    Loading
  },
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~assets/stylus/variable';

.listview {
  position: relative;
  height: 100%;
  overflow: hidden;
  background: $color-background;

  .list-group {
    padding-bottom: 30px;

    .list-group-title {
      padding-left: 20px;
      height: 30px;
      line-height: 30px;
      font-size: $font-size-small;
      color: $color-text-l;
      background: $color-highlight-background;
    }

    .list-group-item {
      display: flex;
      align-items: center;
      padding: 20px 0 0 30px;

      .avatar {
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }

      .name {
        margin-left: 20px;
        color: $color-text-l;
        font-size: $font-size-medium;
      }
    }
  }

  .list-shortcut {
    z-index: 30;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    padding: 20px 0;
    width: 20px;
    border-radius: 20px;
    text-align: center;
    background: $color-background-d;
    font-family: Helvetica;

    .item {
      padding: 3px;
      line-height: 1;
      color: $color-text-l;
      font-size: $font-size-small;

      &.current {
        color: $color-theme;
      }
    }
  }

  .list-fixed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;

    .fixed-title {
      padding-left: 20px;
      height: 30px;
      line-height: 30px;
      font-size: $font-size-small;
      color: $color-text-l;
      background: $color-highlight-background;
    }
  }

  .loading-wrap {
    position: absolute;
    width: 100%;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>
